<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
	<body>
		
		<div id="roleToolbar">
			<div class="roleToolbar_button" style="height: 40px;">
				<a href="#" class="easyui-linkbutton" iconCls="icon-add" onclick="role_openAdd()" plain="true">添加</a>
	            <a href="#" class="easyui-linkbutton" iconCls="icon-edit" onclick="role_openEdit()" plain="true">修改</a>
				<a href="#" class="easyui-linkbutton" iconCls="icon-remove" onclick="role_setState()" plain="true">状态</a>
	            <a href="#" class="easyui-linkbutton" iconCls="icon-remove" onclick="role_remove()" plain="true">删除</a>
	            <a href="#" class="easyui-linkbutton" iconCls="icon-cancel" onclick="role_cancel()" plain="true">取消</a>
	            <a href="#" class="easyui-linkbutton" iconCls="icon-reload" onclick="role_reload()" plain="true">刷新</a>
			</div>
		</div>
		
		<div id="role_dialog" class="easyui-dialog" data-options="closed:true,iconCls:'icon-save'" style="width: 400px; padding: 10px;">
			<form id="role_dialog_form" method="post">
				<table>
					<tr>
						<td width="100" align="right">角色名称：<input type="hidden" id="id" name="id" /> <input type="hidden" id="state" name="state" /></td>
						<td><input type="text" id="roleName" name="roleName" class="easyui-textbox" 
									data-options="required:true,editable:true"/></td>
					</tr>	
					<tr>
						<td width="100" align="right">角色权限：</td>
						<td><input type="text"  id="permissionsId" name="permissions" class="easyui-combotree"
									data-options="url:'/filesharingsystem/permissions_list',method:'get',required:true,multiple:true,panelHeight:133"/>
						</td>
					</tr>
					<tr>
						<td width="100" align="right">文件总数：</td>
						<td><input type="text"  id="fileCount" name="fileCount" class="easyui-numberbox" data-options="min:0,max:1000,required:true,editable:true"/>
						</td>
					</tr>
				</table>
			</form>
		</div>
		<table id="role_dg"></table>
		
		<script type="text/javascript">
			$(function(){
				$('#role_dg').datagrid({
					url:pageContext_request_contextPath+'/roles',
					method:"get",
					//toolbar:"#roleToolbar",
					fitColumns:true,
					pagination:true,
					border:false,
					pageSize:10,
					columns:[[
						{field:"ck",checkbox:true},
						{field:'id',title:'id',width:100},
						{field:'roleName',title:'roleName',width:100},
						{field:'fileCount',title:'fileCount',width:100},
						{field:"state",title:"state",width:200,formatter:function(value,row,index){
							if(value == 1){
								return "正常状态";
							}else{
								return "禁用状态";
							}
						}},
						{field:'permissions',title:'permissions',formatter:function(value,row,index){
							var permissionsStr = "";
							if(value!=null && value.length>0){
								for(var i=0;i<value.length;i++){
									 permissionsStr += "【"+ value[i].resource +"】"
								}
							}
							return  permissionsStr;
						}}
					]]
				});

			})
			
	
			function role_openAdd(){
				$("#role_dialog_form").form("clear");
				$("#state").val(0);
				$("#role_dialog").dialog({
					title:"添加角色",
					closed:false,
					modal:true,
					buttons:[
						{
							text:"确定",iconCls:"icon-ok",
							handler:role_add
						},
						{
							text:"取消",iconCls:"icon-cancel",
							handler:function(){
								$("#role_dialog").dialog("close");
							}
						}
					],
				});
			}
			
			function role_openEdit() {
				$("#role_dialog_form").form("clear");
				var rows = $("#role_dg").datagrid("getSelections");
				if(rows.length > 1){
					$.messager.alert("信息提示","一次只能修改一条数据！","info");
				}else if(rows.length == 0){
					$.messager.alert("信息提示","请勾选您要修改的数据！","info");
				}else{
					
					var role = rows[0];
					$("#role_dialog_form").form("load",role); // 选中的行的数据绑定到form表单上	
					$("#id").val(role.id);
					$("#state").val(role.state);
					$("#role_dialog").dialog({
						title:"修改角色",
						closed:false,
						modal:true,
						buttons:[
							{text:"确定",iconCls:"icon-ok",handler:role_edit},
							{text:"取消",iconCls:"icon-cancel",handler:function(){
								$("#role_dialog").dialog("close");}},
						]
					});
				}
			}
			
			
			function role_remove() {
				var rows = $("#role_dg").datagrid("getSelections");
				if(rows.length < 1){
					$.messager.alert("信息提示","请勾选您要删除的数据！","info");
				}else{
					$.messager.confirm("信息提示","确定要删除选中的记录吗？",function(result){
						if(result){
							var ids = [];
							$(rows).each(function(){
								ids.push(this.id);
							});
							$.ajax({
								url:"/filesharingsystem/roles",
								method:"delete",
								contentType: "application/json;charset=utf-8",
								data: JSON.stringify(ids),
								success:function(data){ //"ok","error"
										role_reload();
									},
								error:function(){
									$.messager.alert("信息提示","删除失败！","info");
								}
							});
						}
					});
				}
			}

			function role_setState(){
				var rows = $("#role_dg").datagrid("getSelections");
				
				if(rows.length == 0){
					$.messager.alert("信息提示","请勾选您要修改的数据！","info");
				}else{
					$.messager.confirm("信息提示", "确定要改变选中的角色状态吗？", function(result) {
						if (result) {
							var ids = [];
							$(rows).each(function() {
								ids.push(this.id);
							});
							$.ajax({
								url: "/filesharingsystem/roles",
								method: "put",
								contentType: "application/json;charset=utf-8",
								data: JSON.stringify(ids),
								success: function(data) { //"ok","error"
									if (data == "ok") {
										role_reload();
									} else {
										$.messager.alert("信息提示", "修改失败！", "info");
									}
								}
							});
						}
					});				
				}
			}

			function role_add() {
				//alert($(#rolesId).value);
				doAjax("/filesharingsystem/role","post");
			}
			
			function role_edit(){
				doAjax("/filesharingsystem/role","put");
			}
			
			
			
			
			function doAjax(param,_method){
				//先拿到rolesId：1,2,3  --->  [{id:1},{id:2}]
				var permissionsArr = $("#permissionsId").val().split(',');
				var permissionsObjStr = "[";
				for(var i=0;i<permissionsArr.length;i++){
					permissionsObjStr += "{id:'"+permissionsArr[i]+"'},";
				}
				//[{id:1},{id:2},{id:3}]
				permissionsObjStr = permissionsObjStr.substring(0,permissionsObjStr.length-1) + "]";
				var permissionsObj = eval("(" + permissionsObjStr + ")");
				$.ajax({
					url:param,
					method:_method,
					contentType:"application/json;charset=utf-8",
					data:JSON.stringify({
						id:$("#id").val(),
						roleName:$("#roleName").val(),
						state:$("#state").val(),
						permissions: permissionsObj,
						fileCount:$("#fileCount").val(),
						users:""
					}),
					success:function(data){ //"ok","error"
						$("#role_dialog").dialog("close");
						role_reload();
						
					},
					error:function(){
						$.messager.alert("信息提示","提交失败！");
					}
				});
			}
			
			function role_cancel() {
				$("#role_dg").datagrid("rejectChanges");
			}
			
			function role_reload() {
				$("#role_dg").datagrid("reload");
			}
		</script>
		
	</body>
</html>



